#ifndef SERVER_H
#define SERVER_H

#include <QObject>
#include <QtNetwork>
#include <QList>
#include <QTcpSocket>
#include <QTcpServer>
#include "clientsocket.h"
/**
 *TCP服务端
 * @brief The Server class
 */
class Server : public QTcpServer
{
    Q_OBJECT
public:
    explicit Server(quint16 port, QObject * parent =  nullptr);
    //开启监听端口
    bool startListen();
    //新的连接到达自动调用该函数
    void incomingConnection(qintptr handle) override;

    ClientSocket * getCurrentClientSocket(){
        return currentClientSocket;
    }
    void setCurrentClientSockete(ClientSocket * clientSocket){
        this->currentClientSocket = clientSocket;
    }
signals:
    //新连接
    void signalNewConnection(QString client);
    //收到数据
    void signalReceiveData(QString client, QString msg);
    //连接断开
    void signalDisconnection(QString client);

public slots:
    //接收客户端连接断开的通知
    void slotClientDisconnected(ClientSocket * socket);
    //接收读到的数据
    void slotReceiveData(ClientSocket * socket, QString msg);
private:

    //端口 默认8080
    quint16 port = 8080;
    //客户端socket
    QList<ClientSocket*>  clientSocketList;
    //当前发消息的客户端
    ClientSocket * currentClientSocket = nullptr;
};

#endif // SERVER_H
